﻿using A.M.ETO;
using DotNetCore.CAP;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;

namespace L.M.System.Read.API.Controllers
{
    /// <summary>
    /// 系统编号控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class System_number_tableReadController : ControllerBase
    {
        private readonly ISystem_number_tableServices system_Number_TableServices;
        public System_number_tableReadController(ISystem_number_tableServices system_Number_TableServices)
        {
            this.system_Number_TableServices = system_Number_TableServices;
        }

        /// <summary>
        /// 异步获取系统列表
        /// </summary>
        /// <returns>获取系统列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetSystem_number_tableRead()
        {
            var list = await system_Number_TableServices.GetShowInforAsync();
            return Ok(list);
        }

        /// <summary>
        /// 系统编号查询 + 分页 
        /// </summary>
        /// <param name="tName">模糊查询</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns>系统编号查询 + 分页 </returns>
        [HttpGet]
        public async Task<IActionResult> GetSystem_number_table(int pageSize, int pageIndex, string? tName)
        {
            var list = await system_Number_TableServices.GetSystem_number_table(pageSize, pageIndex, tName);
            return Ok(list);
        }


        /// <summary>
        /// 修改系统的编号确保系统为最先编号
        /// </summary>
        /// <param name="NumberInfor"></param>
        /// <returns></returns>
        [HttpGet]
        [CapSubscribe("GetNewNumber")]
        public IActionResult GetNewNumber(string? NumberInfor)
        {
            var res = system_Number_TableServices.GetNewNumber(NumberInfor);
            return Ok(res);
        }

        /// <summary>
        /// 获取自己编号
        /// </summary>
        /// <param name="sName">系统名称</param>
        /// <returns>获取自己编号</returns>
        [HttpGet]
        public IActionResult GetMyNumber(string? sName)
        {
            var res = system_Number_TableServices.GetShowObj(c => c.system_name == sName);
            return Ok(res);
        }

        /// <summary>
        /// 修改系统前缀
        /// </summary>
        /// <param name="systemUpdDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdSystem(system_number_table systemUpdDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            if (systemUpdDTO.system_prefix.Length > 5)
            {
                apiReturn.Code = 500;
                apiReturn.Message = "最长不可超过五位";
                return Ok(apiReturn);
            }
            var res = system_Number_TableServices.GetShowObj(c => c.system_number == systemUpdDTO.system_number);
            if (res != null)
            {
                res.system_prefix = systemUpdDTO.system_prefix;
                res.system_number_serial = res.system_number_serial.Replace(res.system_prefix, systemUpdDTO.system_prefix);
                var a = await system_Number_TableServices.UpdInfor_Async(res);
                if (a > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "修改成功";
                }
                else
                {
                    apiReturn.Code = 500;
                    apiReturn.Message = "修改失败";
                }
            }
            return Ok(apiReturn);


        }

        /// <summary>
        /// 每日定时修改编号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> UpdSystemInfor()
        {
            return Ok(await system_Number_TableServices.UpdSystemInfor());
        }
    }
}
